<?php

include_once './wx_log.class.php';

class WxAbility{

    public function MsgReply()
    {
        $xml = file_get_contents('php://input');

        if($xml){
            $log = new WxLog();
            $log::writeLog($xml, 1);
    
            $obj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
            $MsgType = $obj->MsgType;
            $fun = $MsgType .'Fun';
            echo $result = call_user_func([$this, $fun], $obj);
            $log::writeLog($result, 2); 
        }
        
    }

    /**
     *  文本回复
     */
    private function textFun($obj)
    {
        $keyword = ['', '图片'];
        $index = array_search($obj->Content, $keyword);
        if($index){
            return $this->createText($obj, $keyword[$index], '');
        }else{
            $content = '未能匹配到关键字 ---> ' . $obj->Content;
            return $this->createText($obj, $keyword[0], $content);
        }
    }

    /**
     *  创建文本
     */
    private function createText($obj, $type, $content)
    {
        switch($type){
            case '图片':
                $xml = "<xml><ToUserName><![CDATA[%s]]></ToUserName>" . "\n" .
                       "<FromUserName><![CDATA[%s]]></FromUserName>" . "\n" .
                       "<CreateTime>%s</CreateTime>" . "\n" .
                       "<MsgType><![CDATA[image]]></MsgType>" . "\n" .
                       "<Image>" . "\n" .
                       "    <MediaId><![CDATA[24rGpP11wjYvHt2Ew-LLDlFlaqvmXl_x_bU7OwTQbxHOAm7vr08I5NkDeFAqXzZ6]]></MediaId> " . "\n" .
                       "</Image>" . "\n" .
                       "</xml>";

                $str = sprintf($xml, $obj->FromUserName, $obj->ToUserName, time());       
                break;
            default: 
                $xml = "<xml><ToUserName><![CDATA[%s]]></ToUserName>" . "\n" .
                       "<FromUserName><![CDATA[%s]]></FromUserName>" . "\n".
                       "<CreateTime>%s</CreateTime>" . "\n" .
                       "<MsgType><![CDATA[text]]></MsgType>" . "\n" .
                       "<Content><![CDATA[%s]]></Content>" . "\n" .
                       "</xml>";
            
                $str = sprintf($xml, $obj->FromUserName, $obj->ToUserName, time(), $content);
                break;    
        }
        
        return $str;
    }

    /**
     *  事件
     */

    private function eventFun($obj)
    {
        switch($obj->Event){
            case 'CLICK':
                if($obj->EventKey == 'key001'){
                    $str = '点击了一级菜单';
                }elseif($obj->EventKey == 'key002'){
                    $content = '我是客服，请问有什么问题？';
                    return $this->createText($obj, '', $content);
                }
                elseif($obj->EventKey == 'key003'){
                    $xml = "<xml><ToUserName><![CDATA[%s]]></ToUserName>" . "\n" .
                           "<FromUserName><![CDATA[%s]]></FromUserName>" . "\n" .
                           "<CreateTime>%s</CreateTime>" . "\n" .
                           "<MsgType><![CDATA[event]]></MsgType>" . "\n" .
                           "<Event><![CDATA[LOCATION]]></Event>" . "\n" .
                           "<Latitude>23.137466</Latitude>" . "\n" .
                           "<Longitude>113.352425</Longitude>" . "\n" .
                           "<Precision>119.385040</Precision>" . "\n" .
                           "</xml>";

                    $str = sprintf($xml, $obj->FromUserName, $obj->ToUserName, time());      
                }else{
                    $str = '点击了三级菜单';
                }
                break;
            case 'subscribe':
                $xml = "<xml><ToUserName><![CDATA[%s]]></ToUserName>" . "\n" .
                       "<FromUserName><![CDATA[%s]]></FromUserName>" . "\n".
                       "<CreateTime>%s</CreateTime>" . "\n" .
                       "<MsgType><![CDATA[text]]></MsgType>" . "\n" .
                       "<Content><![CDATA[%s]]></Content>" . "\n" .
                       "</xml>";
            
                $str = sprintf($xml, $obj->FromUserName, $obj->ToUserName, time(), '你已经关注了我们的公众号！');

                $str .= "\n" . '用户：' . $obj->FromUserName . ' 关注了公众号' . "\n" . 'ID：' . $obj->FromUserName;
                break;
            case 'unsubscribe':
                $str = '用户：' . $obj->FromUserName . ' 取消关注了公众号' . "\n" . 'ID：' . $obj->FromUserName;
                break;
        }

        return $str;
    } 
}